package com.iamalive;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;


public class LocalCache {
	public boolean mUserValidated;
	public String mTokenId;
	
	public class UserDetails {
		public long id;
		public String name;
		public String emailId;
		public String password;
		public String address;
		public String city;
		public String state;
		public String country;
		public String zip;
		public String phone;
		public String gender;
		public String imId;
		public String imService;
		public Date dob;
		public Date doj;
		public String dola;
		public String regnStatus;
	}
	
	public class RecipientDetails {
		public long id;
		public String name;
		public String address;
		public String phone;
		public String email;
		public String relationship;
		public String toString() {
			return name;
		}
	}
	
	public enum MediaType {
		TEXT,
		TEXT_FILE,
		IMAGE_FILE,
		AUDIO_FILE,
		VIDEO_FILE
	}
	
	public class InfoDetails {
		public long id;
		public String infoBody;
		public MediaType media;
		public Date doe;
		public long ttl;
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return infoBody;
		}
		
		public ArrayList<String> listOfRecipients;
		public InfoDetails() {
			listOfRecipients = new ArrayList<String>();
		}
	}
	
	public UserDetails mUserDetails;
	public TreeMap <String, RecipientDetails> mRecipientDetailsMap;
	public ArrayList<InfoDetails> mInfoList;

	
	private static LocalCache mLocalCacheInstance;
	private LocalCache() {
		mUserValidated = false;
		mUserDetails = new UserDetails();
		mRecipientDetailsMap = new TreeMap <String, RecipientDetails> ();
		mInfoList = new ArrayList<InfoDetails>();
	}
	
	public static LocalCache getSingleInstance() {
	    if (mLocalCacheInstance == null) {
	    	mLocalCacheInstance = new LocalCache();
	    }
	    return mLocalCacheInstance;
	}
	
	public ArrayList<RecipientDetails> getRecipientList() {
		ArrayList<RecipientDetails> recipientList = new ArrayList<RecipientDetails>();
			
		Iterator<Entry<String, RecipientDetails>> entries = mRecipientDetailsMap.entrySet().iterator();
		while (entries.hasNext()) {
		    TreeMap.Entry entry = (TreeMap.Entry) entries.next();
		    RecipientDetails value = (RecipientDetails)entry.getValue();
		    recipientList.add(value);		    
		}
		
		return recipientList;
	}
	
	public boolean doesRecipientAlreadyExist(String name) {
		if(mRecipientDetailsMap.containsKey(name)) {
			return true;
		}
		
		return false;
	}
	
	public boolean addNewRecipient(RecipientDetails recipient) {

		if(mRecipientDetailsMap.containsKey(recipient.name)) {
			return false;
		}

		mRecipientDetailsMap.put(recipient.name, recipient);
		return true;
	}
	
	public ArrayList<String> getRecipientNameList() {
		ArrayList<String> recipientNameList = new ArrayList<String>();
		
		Iterator<Entry<String, RecipientDetails>> entries = mRecipientDetailsMap.entrySet().iterator();
		while (entries.hasNext()) {
		    TreeMap.Entry entry = (TreeMap.Entry) entries.next();
		    String recipientName = ((RecipientDetails)entry.getValue()).name;
		    recipientNameList.add(recipientName);		    
		}
		
		return recipientNameList;
	}
	
	public void deleteInfo(int position) {
		mInfoList.remove(position);
	}
}
	
